package com.huatai.exchange.bootup.controller;

import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.BaseResp;
import com.faneui.common.utils.BeanMapper;
import com.faneui.common.utils.ListUtils;
import com.google.common.collect.Lists;
import com.huatai.exchange.dal.model.ExcDataFileInfo;
import com.huatai.exchange.dal.model.ExcExchangeDataFile;
import com.huatai.exchange.facade.DataFileInfoController;
import com.huatai.exchange.facade.bean.DataFileInfoVo;
import com.huatai.exchange.facade.bean.QueryDataFileInfoVoListResp;
import com.huatai.exchange.service.ExcDataFileInfoService;
import com.huatai.exchange.service.ExcExchangeDataFileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "file")
@Api("文件信息")
public class DataFileInfoControllerImpl extends AbstractRestController implements DataFileInfoController {

    @Autowired
    private ExcDataFileInfoService excDataFileInfoService;

    @Autowired
    private ExcExchangeDataFileService excExchangeDataFileService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public QueryDataFileInfoVoListResp queryDataFileInfoVoList(@ModelAttribute DataFileInfoVo dataFileInfoVo) throws Exception {
        QueryDataFileInfoVoListResp resp = new QueryDataFileInfoVoListResp();
        try {
            ExcDataFileInfo param = BeanMapper.map(dataFileInfoVo, ExcDataFileInfo.class);
            List<ExcDataFileInfo> infos = excDataFileInfoService.queryExcDataFileInfoList(param);
            if (ListUtils.isNotEmpty(infos)) {
                List<DataFileInfoVo> vos = Lists.newArrayList() ;
                for(ExcDataFileInfo info:infos){
                    DataFileInfoVo vo = BeanMapper.map(info,DataFileInfoVo.class) ;
                    List<ExcExchangeDataFile> files = excExchangeDataFileService.queryExcExchangeDateFileListByDataFileId(vo.getId()) ;
                    Integer num = ListUtils.isEmpty(files)? 0 :files.size() ;
                    vo.setUserNum(num);
                    vos.add(vo) ;
                }
                resp.setDataFileInfoVos(vos);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @Override
    public BaseResp modifyDataFileInfoVo(DataFileInfoVo dataFileInfoVo) throws Exception {
        BaseResp resp = new BaseResp();
        try {
            ExcDataFileInfo excDataFileInfo = BeanMapper.map(dataFileInfoVo,ExcDataFileInfo.class) ;
            excDataFileInfoService.modifyExcDataFileInfo(excDataFileInfo) ;
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


}
